package org.finesys.boot.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.finesys.common.core.exception.ErrorCodes;
import org.finesys.common.core.module.R;
import org.finesys.common.core.util.MessageUtil;
import org.finesys.system.api.dto.SysUserDTO;
import org.finesys.system.api.dto.UserInfo;
import org.finesys.system.api.entity.SysUser;
import org.finesys.system.api.service.UserService;
import org.finesys.system.service.SysUserService;
import org.springframework.stereotype.Service;

/**
 * feign用户获取接口
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
    private final SysUserService userService;

    /**
     * 通过用户名查找用户信息
     *
     * @param sysUserDTO 用户信息
     * @return 用户信息
     */
    public R<UserInfo> info(SysUserDTO sysUserDTO) {
        String username = sysUserDTO.getUsername();
        String phone = sysUserDTO.getPhone();
        SysUser user = userService.getOne(Wrappers.<SysUser>query()
                .lambda()
                .eq(StrUtil.isNotBlank(username), SysUser::getUsername, username)
                .eq(StrUtil.isNotBlank(phone), SysUser::getPhone, phone));
        if (user == null) {
            return R.failed(MessageUtil.getMessage(ErrorCodes.SYS_USER_USERINFO_EMPTY, username));
        }
        return R.success(userService.getUserInfo(user));
    }
}
